import Common from '../common'
import echarts from 'echarts'
import axios from 'axios'
import region from '../../utils/cityTencent.json'
var Js = Common('data')
export default {
  name: 'data',
  mixins: [Js],
  components: {},
  data() {
    return {
      fields: [{
          key: 'id',
          label: 'ID',
          application: [],
          componentName: 'sls-input',
          listType: '',
          width: 50
        },
        {
          key: 'order_no',
          label: '订单NO',
          application: [],
          componentName: 'sls-input',
          listType: 'normal',
          width: 150,
          header_search: {
            componentName: 'sls-input',
            style: 'width:160px;margin-right:2px;',
            placeholder: '请输入订单NO',
            clearable: true,
            defaultValue: '',
            optionsName: '',
            changeFunc: function(val, self) {
              if (val) {
                self.searchItem.order_no = ['LIKE', ['%' + val + '%']];
              } else {
                delete self.searchItem.order_no;
              };
            },
          },
        },
        {
          key: 'price',
          label: '订单总金额',
          application: [],
          componentName: 'sls-input',
          listType: 'normal',
          width: 120
        },
        {
          key: 'balance',
          label: '奖励金抵扣',
          application: [],
          componentName: 'sls-input',
          listType: 'normal',
          width: 120
        },
        {
          key: 'count',
          label: '数量',
          application: [],
          componentName: 'sls-input',
          listType: 'normal',
          width: 100
        },
        {
          key: "pay_type",
          label: '支付方式',
          listType: 'normal',
          formatter: function(val, tests) {
            return ['微信支付', '货到付款', '银行卡转账', '二维码收款'][val.pay_type];
          },
          componentName: 'sls-select',
        },
        {
          key: "pay_status",
          label: '支付状态',
          application: ['编辑'],
          type: 'select',
          listType: 'normal',
          formatter: function(val, tests) {
            return ['退款', '否', '是'][val.pay_status + 1];
          },
          componentName: 'sls-select',
          optionsName: 'payOptions',
          filter_multiple: false,
          listType: 'normal',
          defaultProps: {
            label: 'text',
            value: 'value',
          },
          placeholder: '请选择是否支付',
          width: 80,
          header_search: {
            componentName: 'sls-select',
            defaultValue: '',
            optionsName: 'payOptions',
            style: 'width:160px;margin-right:2px;',
            placeholder: '请选择是否支付',
            changeFunc: function(val, self) {
              if (val || val === 0) {
                self.searchItem.pay_status = val;
              } else {
                delete self.searchItem.pay_status;
              };
            },
          }
        },
        {
          key: "transport_status",
          label: '配送状态',
          application: ['编辑'],
          listType: 'normal',
          formatter: function(val, tests) {
            return ['待配送', '待收货', '已完成'][val.transport_status];
          },
          componentName: 'sls-select',
          optionsName: 'transportOptions',
          filter_multiple: false,
          defaultProps: {
            label: 'text',
            value: 'value',
          },
        },
        {
          key: "order_step",
          label: '订单状态',
          application: [],
          type: 'select',
          listType: 'normal',
          formatter: function(val, tests) {
            if (val.order_step == -1) {
              return '拒绝撤单'
            } else {
              return ['正常下单', '申请撤单', '已撤单', '完结'][val.order_step];
            }
          },
          componentName: 'sls-select',
          optionsName: 'stepOptions',
          filter_multiple: false,
          defaultProps: {
            label: 'text',
            value: 'value',
          },
        },
        {
          key: 'passage1',
          label: '推广业务员',
          componentName: 'sls-input',
          listType: 'normal',
          formatter: function(val) {
            return val.staff.login_name ? val.staff.login_name + '【' + val.passage1 + '】' : val.passage1
          }
        },
        {
          key: "express_company",
          label: '快递公司',
          application: ['编辑'],
          componentName: 'sls-input',
          // componentName: 'sls-select',
          // optionsName: 'expressOptions',
          // defaultProps: {
          //   label: 'title',
          //   value: 'title',
          // }
        },
        {
          key: 'express_no',
          label: '快递单号',
          application: ['编辑'],
          componentName: 'sls-input',
        },
        {
          key: 'user_no',
          label: '用户NO',
          application: [],
          componentName: 'sls-input',
          listType: 'normal',
        },
        {
          key: 'name',
          label: '用户昵称',
          application: [],
          componentName: 'sls-input',
          listType: 'normal',
          formatter: function(val, tests) {
            return val.user.nickname ? val.user.nickname : '';
          },
        },
        {
          key: 'remark',
          label: '备注',
          application: ['编辑'],
          componentName: 'sls-input',
          listType: 'normal',
        },
        {
          key: 'create_time',
          label: '创建时间',
          listType: 'normal',
          placeholder: '请选择创建时间',
          header_search: {
            componentName: 'sls-date-time-range',
            style: 'width:160px;margin-right:2px;',
            start_placeholder: '创建开始时间',
            end_placeholder: '创建结束时间',
            defaultValue: '',
            changeFunc: function(value, self) {
              if (!value) {
                delete self.searchItem.create_time;
              } else {
                self.searchItem.create_time = ['between', value = value.map(function(e) {
                  return e / 1000;
                })]
              };

            },
          },
          width: 200,
        },
        {
          label: '操作',
          listType: 'deal',
          width: 200
        },
      ],
      btn_info: [],

      visitorPaginate: {
        count: 0,
        currentPage: 1,
        pagesize: 10,
        is_page: true,
        page_sizes: [10, 30, 60, 90],
        layout: 'total, sizes, prev, pager, next, jumper',
      },
      paginate: {
        count: 0,
        currentPage: 1,
        pagesize: 10,
        is_page: true,
        page_sizes: [10, 30, 60, 90],
        layout: 'total, sizes, prev, pager, next, jumper',
      },
      searchItem: {
        behavior: 1,
        transport_status: 0
      },
      startSearchItem: {
        behavior: 1,
        transport_status: 0
      },
      optionData: {
        labelOptions: [],
        typeOptions: [{
          text: '未处理',
          value: 0
        }, {
          text: '已处理',
          value: 1
        }],
      },
      otherData: {},
      UserInfo: {
        tableName: 'UserInfo',
        searchItem: {},
        fixSearchItem: {
          status: 1
        },
        key: 'user_no',
        middleKey: 'user_no',
        condition: 'in',
      },

      user: {},
      isSign: false,
      visitorData: [],
      echartsOptions: {},
      loading: false,


      weatherTxt: '',
      weather: {},
      weatherData: {},

      echartsOptions1: null,
      echartsOptions2: null,
      echartsOptions3: null,

      rankData: [],
      regionOptions: [],
      timeArr: [],
      timeArr1: [],
      title: '',
      title1: '',
      region: [],
      region1: [],

      mainData: [],
      mainData1: [],

      userOptions: [],
      user_no: '',
      typeOptions: [{
          text: '个人业绩',
          value: 1
        },
        {
          text: '下属业绩',
          value: 2
        },
      ],
      type: 1,
      arr: [],
      brr: [],
      compute: {},

      table_arguments: {
        height: '70%',
        row_key: 'id',
        tree_props: {
          children: 'child',
          child: 'child',
          hasChildren: 'child'
        },
        default_expand_all: false,
        expand: true,
        selection: true,
        cell_style: {}
      },
    }

  },

  mounted() {
    this.regionOptions = region;
    this.init()
  },
  computed: {
    token: function() {
      return this.$store.getters.getToken
    }
  },
  methods: {


    timeChange(e) {
      const self = this;
      if (e.value && e.value != null) {
        self.timeArr = e.value;
      } else {
        self.timeArr = [];
      };
      console.log('timeChange', e)
    },

    /**
     * 初始化
     */
    init() {
      // this.timeArr = this.getMonth(Date.now());
      // this.timeArr1 = this.getMonth(Date.now());
      this.initUserData();
      // this.statisticTwo();
    },

    /**
     *
     */

    getMonth(date) {
      let inDate = new Date(date)
      let year = inDate.getFullYear()
      let month = inDate.getMonth()
      let startTime = new Date(year, month, 1).getTime()
      let endTime = new Date(year, month + 1, 1).getTime() - 1
      return [parseInt(startTime), parseInt(endTime)]
    },

    async initUserData(isNew) {

      const self = this;
      const postData = {
        searchItem: {
          role: 12
        }
      };
      postData.token = self.$store.getters.getToken;
      var res = await self.$$api_userGet({
        data: postData
      });
      self.userOptions = res.info.data;
      self.user_no = self.userOptions[0].user_no;
      self.initMainData();
    },

    async initMainData(isNew) {
      const self = this;
      const postData = {
        searchItem: {
          parent_no: self.user_no,
          level: ['in', [1, 2]]
        },
        getAfter: {
          childUser: {
            tableName: 'User',
            middleKey: 'child_no',
            key: 'user_no',
            condition: '=',
            info: ['headImgUrl', 'mainImg', 'login_name', 'nickname']
          }
        },
        getAfterCompute: {
          orderPrice: {
            tableName: 'Order',
            field: 'price',
            method: 'sum',
            condition: '=',
            key: 'user_no',
            middleKey: 'child_no',
            searchItem: {
              status: 1,
              pay_status: 1,
              order_type: 1,
              level: 0
            },
          },
        }
      };
      postData.token = self.$store.getters.getToken;
      var res = await self.$$api_distributionGet({
        data: postData
      });
      self.mainData = res.info.data;
      self.arr = [];
      self.brr = [];
      self.mainData.forEach((item, index) => {
        if (item.level == 1) {
          self.arr.push(item.child_no);
        } else {
          self.brr.push(item.child_no);
        }
      })
      self.initOrderData(true);
    },

    //业绩统计
    async initOrderData(isNew) {
      const self = this;
      if (isNew) {
        self.paginate.currentPage = 1;
      };
      const postData = {
        searchItem: {
          passage1: self.type == 1 ? self.user_no : ['in', self.arr.length>0?self.arr:['--']],
          level: 0,
          order_type: 1,
          pay_status: 1
        },
      };
      postData.getAfter = {
        user: {
          tableName: 'User',
          middleKey: 'user_no',
          key: 'user_no',
          condition: '=',
          searchItem: {
            status: 1
          },
          info: ['nickname'],
        },
        staff: {
          tableName: 'User',
          middleKey: 'passage1',
          key: 'user_no',
          condition: '=',
          searchItem: {
            status: 1
          },
          info: ['login_name', 'nickname', 'name'],
        }
      };
      postData.compute = {
        allPrice1: ['Order','sum','price',{passage1: self.user_no,order_type: 1,pay_status:1,level: 0}],
        allPrice2: ['Order','sum','price',{passage1: ['in',self.arr.length>0?self.arr:['--']],order_type: 1,pay_status:1,level: 0}],
      }
      if(self.timeArr.length>0){
        postData.searchItem.create_time = ['between', [self.timeArr[0]/1000, self.timeArr[1]/1000]];
        postData.compute.allPrice1[3].create_time = ['between', [self.timeArr[0]/1000, self.timeArr[1]/1000]];
        postData.compute.allPrice2[3].create_time = ['between', [self.timeArr[0]/1000, self.timeArr[1]/1000]];
      }
      postData.paginate = self.$$cloneForm(self.paginate);
      postData.token = self.$store.getters.getToken;
      var res = await self.$$api_orderGet({
        data: postData
      });
      self.compute  = res.info.compute;
      self.mainData1 = res.info.data;
      self.paginate.count = res.info.total;
    },


    /**
     * 列表主函数
     */



    async excelOutput() {
      const self = this;
      const postData = {
        s_time: parseInt(self.timeArr1[0] / 1000),
        e_time: parseInt(self.timeArr1[1] / 1000),
        region: self.region1.length > 0 ? self.region1.join(',') : '',
        title: self.title1,
      };
      postData.token = self.$store.getters.getToken;
      postData.excelOutput = {
        expTitle: '产品销售额/销量统计【' + self.$$timeto(self.timeArr1[0], 'ymd') + '至' + self.$$timeto(self.timeArr1[1],
          'ymd') + '】',
        expCellName: [{
            title: '商品名称',
            key: ['title'],
            type: 'string',
          },
          {
            title: '销售额',
            key: ['sumPrice'],
            type: 'string',
          },
          {
            title: '销量',
            key: ['sumCount'],
            type: 'string',
          },
        ],
        fileName: 'test'
      };
      // var res = await self.$$api_statisticTwo({
      // 	data: postData
      // });
      // $$api_statisticTwo
      window.open(
        'https://testtwo.solelyplus.com/680/public/index.php/api/v1/Project/Solely/statisticTwo?excelOutput=' +
        JSON.stringify(postData.excelOutput) +
        '&s_time=' + postData.s_time +
        '&e_time=' + postData.e_time +
        '&region=' + postData.region +
        '&title=' + postData.title)
    },

  },


}
